package com.lanou.beautifulwardrobe.brand.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;

import java.util.List;

/**
 * Created by dllo on 15/11/5.
 */
public class BrandListViewAdapter extends BaseAdapter {

    private List<ChoiceProduct> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private ImageLoader imageLoader;

    public void addData(List<ChoiceProduct> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BrandViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.showproducts, null);
            holder = new BrandViewHolder();
            holder.showProductIv = (NetworkImageView) convertView.findViewById(R.id.showProductIv);
            holder.showProductPriceTv = (TextView) convertView.findViewById(R.id.showProductPriceTv);
            convertView.setTag(holder);
        } else {
            holder = (BrandViewHolder) convertView.getTag();
        }

        if (datas != null && datas.size() > 0) {
            holder.showProductPriceTv.setText(datas.get(position).getTaobao_selling_price());
          //  Log.i("lanou", datas.get(position).getTaobao_pic_url() + "//");
            getBitMap(datas.get(position).getTaobao_pic_url(), holder.showProductIv);

        }
        return convertView;
    }



    class BrandViewHolder {
        private NetworkImageView showProductIv;
        private TextView showProductPriceTv;

    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
